import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule } from '@nestjs/swagger';
import { ConfigService } from '@nestjs/config';
import { Logger } from '@nestjs/common';
import * as ip from 'ip';
import chalk from 'chalk';
import { swaggerConfig } from './config/swagger.config';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const configService = app.get(ConfigService);
  const logger = new Logger('Bootstrap');

  // Swagger 配置
  const document = SwaggerModule.createDocument(app, swaggerConfig);
  SwaggerModule.setup('api', app, document);

  // 启动应用
  const port = configService.get<number>('app.port') || 3000;
  const host = configService.get<string>('app.host') || 'localhost';
  await app.listen(port, host);

  // 输出访问地址
  const localIp = ip.address();
  logger.log(
    `${chalk.green('🚀')} Application is running on: ${chalk.blue(`http://${host}:${port}`)}`,
  );
  logger.log(
    `${chalk.green('📚')} Swagger UI is available at: ${chalk.blue(`http://${host}:${port}/api`)}`,
  );
  logger.log(
    `${chalk.green('🌐')} Local IP access: ${chalk.blue(`http://${localIp}:${port}`)}`,
  );
  logger.log(
    `${chalk.green('📖')} Local Swagger UI: ${chalk.blue(`http://${localIp}:${port}/api`)}`,
  );
}
bootstrap();